import  pymongo
import  datetime

client = pymongo.MongoClient('127.0.0.1', 27018)
print(client.server_info())
col = client['mongo']['col']
r = col.find_one()
print(r)

test = client['mongo']['test']
test.insert_one({"name":"dsc",'date':datetime.datetime.now()})

r = test.find(filter={'name':'dsc'},
              projection={'_id':False,'name':True,'date':True},
              sort=[('date',pymongo.ASCENDING)]).limit(5).skip(2)
for i in r:
    print(i)

ag = test.aggregate(pipeline=[
    {'$match':{'name': 'dsc'}},
    {'$group':{'_id':'$name','count':{'$sum':1},'min':{'$min':'$date'}}}])
for i in ag:
    print(i)
'''
update_one()  upsert=false  # 更新
update_many() 

delete_one()                # 删除
delete_many()

find_one()  projection 指定字段  sort 排序
find()   

通过 _id 查询
from bson.objectid import OjbectId
condition = {'_id': ObjectId(id)}

find_one_and_delete()

count_documents()  # 计数

create_index('name', unique=True) 

list_indexes()     # 索引
index_infomation()
drop_index()
drop_indexes()     

collection.drop()  # 删除表

col.find().sort('_id',-1) # 根据插入时间降序


# 根据 _id 查询指定日期插入的数据

比如昨天插入数据
start_day_time = datetime.datetime.today() - datetime.timedelta(1)
end_day_time = datetime.datetime.today() - datetime.timedelta(0)

start_day_zero = datetime.datetime.strptime(start_day_time.strftime("%Y-%m-%d"), "%Y-%m-%d")
end_day_zero = datetime.datetime.strptime(end_day_time.strftime("%Y-%m-%d"), "%Y-%m-%d")

start_dummy_id = ObjectId.from_datetime(start_day_zero)
end_dummy_id = ObjectId.from_datetime(end_day_zero)

results_count = col.find({"_id": {"$gte": start_dummy_id,"$lte":end_dummy_id}}).count()

'''
